package com.slj.controller;

import com.slj.entity.Class;
import com.slj.service.ClassService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/classes")
@CrossOrigin(origins = "*")
@RequiredArgsConstructor
public class ClassController {
    private final ClassService classService;

    @PostMapping
    public ResponseEntity<Class> createClass(@RequestBody Class clazz) {
        return ResponseEntity.ok(classService.createClass(clazz));
    }

    @PutMapping("/{id}")
    public ResponseEntity<Class> updateClass(@PathVariable Integer id, @RequestBody Class clazz) {
        clazz.setClass_id(id);
        return ResponseEntity.ok(classService.updateClass(clazz));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteClass(@PathVariable Integer id) {
        classService.deleteClass(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<Class> getClassById(@PathVariable Integer id) {
        return ResponseEntity.ok(classService.getClassById(id));
    }

    @GetMapping("/name/{name}")
    public ResponseEntity<Class> getClassByName(@PathVariable String name) {
        return ResponseEntity.ok(classService.getClassByName(name));
    }

    @GetMapping
    public ResponseEntity<List<Class>> getAllClasses() {
        return ResponseEntity.ok(classService.getAllClasses());
    }
}